package com.slidingmenu.example.mainview;

import java.net.URLEncoder;

import android.app.AlertDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceScreen;
import android.text.Html;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockPreferenceActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.slidingmenu.example.AttachExample;
import com.slidingmenu.example.LeftAndRightActivity;
import com.slidingmenu.example.PropertiesActivity;
import com.slidingmenu.example.R;
import com.slidingmenu.example.SlidingContent;
import com.slidingmenu.example.SlidingTitleBar;
import com.slidingmenu.example.Util;
import com.slidingmenu.example.ViewPagerActivity;
import com.slidingmenu.example.anim.CustomScaleAnimation;
import com.slidingmenu.example.anim.CustomSlideAnimation;
import com.slidingmenu.example.anim.CustomZoomAnimation;
import com.slidingmenu.example.fragments.FragmentChangeActivity;
import com.slidingmenu.example.responsiveui.ResponsiveUIView;

public class MainView extends SherlockPreferenceActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle("滑动菜单Demos");

		// Crittercism.init(getApplicationContext(),
		// "508ab27601ed857a20000003");
		this.addPreferencesFromResource(R.xml.main);
	}

	@Override
	public boolean onPreferenceTreeClick(PreferenceScreen screen,
			Preference pref) {
		Class<?> cls = null;
		String title = pref.getTitle().toString();
		if (title.equals(getString(R.string.properties))) {
			cls = PropertiesActivity.class;
		} else if (title.equals(getString(R.string.attach))) {
			cls = AttachExample.class;
		} else if (title.equals(getString(R.string.changing_fragments))) {
			cls = FragmentChangeActivity.class;
		} else if (title.equals(getString(R.string.left_and_right))) {
			cls = LeftAndRightActivity.class;
		} else if (title.equals(getString(R.string.responsive_ui))) {
			cls = ResponsiveUIView.class;
		} else if (title.equals(getString(R.string.viewpager))) {
			cls = ViewPagerActivity.class;
		} else if (title.equals(getString(R.string.title_bar_slide))) {
			cls = SlidingTitleBar.class;
		} else if (title.equals(getString(R.string.title_bar_content))) {
			cls = SlidingContent.class;
		} else if (title.equals(getString(R.string.anim_zoom))) {
			cls = CustomZoomAnimation.class;
		} else if (title.equals(getString(R.string.anim_scale))) {
			cls = CustomScaleAnimation.class;
		} else if (title.equals(getString(R.string.anim_slide))) {
			cls = CustomSlideAnimation.class;
		}
		Intent intent = new Intent(this, cls);
		startActivity(intent);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.github:
			Util.goToGitHub(this);
			return true;
		case R.id.about:
			new AlertDialog.Builder(this).setTitle(R.string.about)
					.setMessage(Html.fromHtml(getString(R.string.about_msg)))
					.show();
			break;
		case R.id.licenses:
			new AlertDialog.Builder(this)
					.setTitle(R.string.licenses)
					.setMessage(
							Html.fromHtml(getString(R.string.apache_license)))
					.show();
			break;
		case R.id.contact:
			final Intent email = new Intent(
					android.content.Intent.ACTION_SENDTO);
			String uriText = "mailto:jfeinstein10@gmail.com" + "?subject="
					+ URLEncoder.encode("SlidingMenu Demos Feedback");
			email.setData(Uri.parse(uriText));
			try {
				startActivity(email);
			} catch (Exception e) {
				Toast.makeText(this, R.string.no_email, Toast.LENGTH_SHORT)
						.show();
			}
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.example_list, menu);
		return true;
	}

}
